<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed!');

/**
 *	Oemah Code Lite Cart Library
 *	Database disesuaikan
 *	
 *	@author 	jokowow
 *	@access 	public
 *	@version 	1.0-a
 */

class Litecart
{
	private $_id;
	private $_ci;
	private $_fields;
	private $_struct;

	function __construct($config = array())
	{
		$this->_ci =& get_instance();
		$this->_ci->load->library('session');

		$this->initialize($config);
	}

	function initialize($config = array())
	{
		$this->_id = isset($config['key']) ? $config['key'] : 'id';

		// fields mapping
		if (isset($config['mapping']) && count($config['mapping']) > 0)
		{
			$valid = FALSE;
			foreach($mapping as $name => $map)
			{
				if ($name == $this->_id) $valid = TRUE;
				$this->_struct[] = array(
					'name'  => $name,
					'map'   => $map,
					'value' => NULL
				);
			}
			if ($valid == FALSE)
				trigger_error('No '.$this->_id.' field in your mapping configuration', E_USER_ERROR);
		}
		else
		{
			// testing for swanstwenty
			$this->_struct = array(
				array('name' => 'id', 			'map' => 'p_id', 		'value' => NULL),
				array('name' => 'name', 		'map' => 'p_name', 		'value' => NULL),
				array('name' => 'price', 		'map' => 'p_price', 	'value' => NULL),
				array('name' => 'qty', 			'map' => 'p_qty', 		'value' => NULL),
				array('name' => 'thumb', 		'map' => 'p_thumb', 	'value' => NULL),
				array('name' => 'color', 		'map' => 'p_color', 	'value' => NULL),
				array('name' => 'size', 		'map' => 'p_size', 		'value' => NULL),
				array('name' => 'material', 	'map' => 'p_material', 	'value' => NULL),
				array('name' => 'subcategory', 	'map' => 'p_c_sub_id', 	'value' => NULL)
			);	
		}

		if ( ! $this->_ci->session->userdata('cart'))
			$this->_ci->session->set_userdata('cart', array());
	}

	function read()
	{
		$temp = $this->_ci->session->userdata('cart');
		$data = array();
		
		if (count($temp) > 0)
		{
			foreach($temp as $item)
			{
				$data[] = $this->_convert($item);
			}
		}

		return $data;
	}

	function fetch()
	{
		return $this->_ci->session->userdata('cart');
	}

	function save($cart)
	{
		$this->_ci->session->set_userdata('cart', $cart);
	}

	function reset()
	{
		$this->_ci->session->set_userdata('cart', array());
	}

	private function _is_batch($array)
	{
		return (count($array) != count($array, 1));
	}

	private function _convert($item, $from = 'name', $to = 'map')
	{
		$result = new stdClass();

		foreach($this->_struct as $st)
		{
			foreach($item as $k => $v)
			{
				if ($st[$from] == $k)
					$result->$st[$to] = $v;
			}
		}

		return $result;
	}

	function add($data)
	{
		// $this->reset();

		$cart = $this->fetch();
		
		if (is_array($data))
		{
			if ($this->_is_batch($data))
			{
				while(count($data) > 0)
				{
					$current = array_shift($data);
					$this->add($current);
				}
			}
			else
			{
				$data = (Object) $data;
			}
		}

		// filter data
		$temp = new stdClass();

		foreach($this->_struct as $meta)
		{
			if (property_exists($data, $meta['map']))
				$temp->$meta['name'] = $data->$meta['map'];
		}

		// reset memory
		$data = NULL;

		if (count($cart) > 0)
		{
			// find existsing item
			$exists = FALSE;

			foreach($cart as $item)
			{
				if ($temp->{$this->_id} == $item->{$this->_id})
				{
					$item->qty = (intval($item->qty) + intval($temp->qty));
					$exists = TRUE;
				}
			}

			if ($exists === FALSE)
			{
				$temp->qty = intval($temp->qty);
				$cart[] = $temp;
			}

		}
		else
		{
			$temp->qty = intval($temp->qty);
			$cart[] = $temp;
		}

		// save
		$this->save($cart);

	}

	function total_items()
	{
		$cart  = $this->fetch();
		$total = 0;
		
		if (count($cart) > 0)
		{
			foreach($cart as $item)
			{
				$total += intval($item->qty);
			}
		}

		return $total;
	}

	function total_prices()
	{
		$cart  = $this->fetch();
		// print_r($cart);

		$total = 0;
		if (count($cart) > 0)
		{
			foreach($cart as $item)
			{
				$total += (floatval($item->price) * intval($item->qty));
			}
		}
		return $total;
	}

	function edit($id, $data)
	{
		$cart  = $this->fetch();
		$clone = $this->_convert($data, 'map', 'name');
		// print_r($clone);

		if (count($cart) > 0)
		{
			foreach($cart as $item)
			{
				if ($item->{$this->_id} == $id)
				{
					foreach($clone as $key => $value)
					{
						$item->$key = $value;
					}
				}
			}
		}

		$this->save($cart);

	}

	function get($id, $converted = TRUE)
	{
		$cart = $this->fetch();

		if (count($cart) > 0)
		{
			foreach($cart as $item)
			{
				if ($item->{$this->_id} == $id)
				{
					if ($converted == TRUE)
						return $this->_convert($item, 'name', 'map');
					else
						return $item;
				}
			}
		}

		return NULL;
	}

	function remove($id)
	{
		$cart = $this->fetch();

		if (count($cart) > 0)
		{
			$count = count($cart);

			for ($i = $count - 1; $i >= 0; $i--)
			{
				if ($cart[$i]->{$this->_id} == $id)
				{
					array_splice($cart, $i, 1);
				}
			}
		}
		
		$this->save($cart);

	}

}